package com.meritdata.cloud.data.service.development.service;

import com.meritdata.cloud.data.service.development.model.QTdDataApiParameter;
import com.meritdata.cloud.data.service.development.model.QTdDataApiTable;
import com.meritdata.cloud.data.service.development.model.TdDataApiParameter;
import com.meritdata.cloud.data.service.development.model.TdDataApiTable;
import com.meritdata.cloud.data.service.development.repository.IDataApiParameterDao;
import com.meritdata.cloud.data.service.development.repository.IDataApiTableDao;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DataApiTableService {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private IDataApiTableDao iDataApiTableDao;
    public boolean save(TdDataApiTable category) {
        Date now = new Date();
        category.setUpdateTime(now);
        iDataApiTableDao.save(category);
        return true;
    }

    @Modifying
    public boolean udpate(String id,String tableId,Date updateTime) {
        return iDataApiTableDao.update(Long.valueOf(id),Long.valueOf(tableId),updateTime) >=0;
    }

    public TdDataApiTable findByApiId(Long apiId) {
        QTdDataApiTable qTdDataApiTable = QTdDataApiTable.tdDataApiTable;
        Predicate predicate =qTdDataApiTable.apiId.eq(apiId);
        return jpaQueryFactory.selectFrom(qTdDataApiTable)
                .where(predicate)
                .fetchOne();
    }

    public void deleteByapiId(Long apiId) {
        iDataApiTableDao.deleteByApiId(apiId);
    }
}
